package cn.itcast.web.controller.cargo;


import cn.itcast.domain.cargo.ExtCproduct;
import cn.itcast.domain.cargo.ExtCproductExample;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.BaseController;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/*
附件模块
 */ 
@Controller
@RequestMapping("/cargo/extCproduct")
@Slf4j
public class ExtCproductController extends BaseController{
    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;
    @Autowired
    private FileUploadUtil fileUploadUtil;
    /*
    进入附件列表页面
     */
    @RequestMapping("/list")
    public String list(String contractId,String contractProductId,
                       @RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "5") Integer pageSize)
    {
        log.info("执行查询附件的附件列表:");
        //构造条件对象
        ExtCproductExample example = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = example.createCriteria();
        //条件一（设置附件id）
        criteria.andContractProductIdEqualTo(contractProductId);
        //条件二(设置企业id)
        criteria.andCompanyIdEqualTo(getLoginCompanyId());

        //根据企业id和货物id查询附件信息
        PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(example, pageNum, pageSize);

        //查询所有的生产厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryCriteria = factoryExample.createCriteria();
        factoryCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);


        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("factoryList",factoryList);
        return "cargo/extc/extc-list";
    }

    /*
    附件添加/修改
     */
    @RequestMapping("/edit")
    public String edit(ExtCproduct extCproduct) {
        //设置企业信息
        extCproduct.setCompanyId(getLoginCompanyId());
        extCproduct.setCompanyName(getLoginCompanyName());


        if (StringUtils.isEmpty(extCproduct.getId())) {
            //处理文件上传到七牛云
           /* try {
                String imgUrl = "http://" + fileUploadUtil.upload(productPhoto);
                extCproduct.setProductImage(imgUrl);
            } catch (Exception e) {
                e.printStackTrace();
            }*/
            extCproductService.save(extCproduct);
        } else {
            //有附件id,执行更新
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list?contractId=" + extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }

    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //查询所有的生产厂家
        //创建厂家条件对象
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryCriteria = factoryExample.createCriteria();
        //设置条件
        factoryCriteria.andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        //回显厂家下拉列表数据
        request.setAttribute("factoryList",factoryList);

        //根据附件id查询附件数据
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);

        return "cargo/extc/extc-update";
    }

    //cargo/extCproduct/delete.do
    //删除附件
    @RequestMapping("/delete")
    public String delete(String id) {
        //根据附件id查询附件对象
        ExtCproduct extCProduct = extCproductService.findById(id);
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list?contractId=" + extCProduct.getContractId()+"&contractProductId="+extCProduct.getContractProductId();
    }

}
